<template>
    <div class="confirm_dialog">
        <el-dialog
            v-model="state.show"
            :title="state.title || '提示'"
            :width="state.width || '400px'"
            @closed="onClosed">
            <slot v-if="slots.default" name="default"></slot>
            <div v-else class="content">
                <div class="content_title" :style="{ textAlign: state.textAlign || 'center' }">
                    <el-icon class="confirm_dialog_icon"><WarningFilled /></el-icon>
                    <div :style="{ textAlign: state.contentAlign || 'center' }">
                        <slot v-if="slots.content" name="content"></slot>
                        <div v-else>{{ state.content }}</div>
                    </div>
                </div>
            </div>
            <template #footer>
                <slot v-if="slots.footer" name="footer"> </slot>
                <span v-else>
                    <el-button @click="onClose">{{ state.closeText || '取消' }}</el-button>
                    <el-button type="primary" @click="onCofirm">{{ state.confirmText || '确定' }}</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { reactive, watch, useSlots } from 'vue';

const slots = useSlots();
const props = defineProps({
    data: { type: Object, default: () => ({ show: false }) }
});
const emit = defineEmits(['close', 'closed', 'confirm']);

const state = reactive(props.data);
const onClosed = () => {
    emit('closed');
};
const onClose = () => {
    state.show = false;
    emit('close');
};
const onCofirm = () => {
    emit('confirm');
};
watch(
    () => props.data,
    v => {
        Object.assign(state, v);
    },
    { immediate: true, deep: true }
);
</script>

<style lang="scss" scoped>
.confirm_dialog {
    .content {
        padding: 25px 0;
        display: flex;
        flex-direction: column;
        align-items: center;
        .content_title {
            display: flex;
            text-align: center;
            margin-bottom: 5px;

            font-size: 14px;
            .confirm_dialog_icon {
                margin-right: 9px;
                color: #faad14;
                font-size: 22px;
            }
        }
        .content_desc {
            padding-left: 23px;

            color: #969696;

            font-size: 14px;
            line-height: 20px;
        }
    }
}
</style>
